@echo off
:: 设置唯一标题，用于识别窗口
set "WINDOW_TITLE=NPM Run Dev - D:\study-project\swagger-helper"
title %WINDOW_TITLE%

:: 检查是否已有相同标题的 cmd 窗口运行
tasklist /FI "WINDOWTITLE eq %WINDOW_TITLE%*" /FI "IMAGENAME eq cmd.exe" | find /i "cmd.exe" >nul
if %errorlevel% equ 0 (
    echo 脚本已在运行，尝试聚焦窗口...
    call :FocusWindow "%WINDOW_TITLE%"
    exit /b
)

:: 如果没有运行，则启动 npm run start
cd /d "D:\study-project\swagger-helper"
npm run start
exit /b

:: 聚焦已存在的窗口（使用 PowerShell）
:FocusWindow
setlocal
set "TITLE=%~1"
powershell -command "[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic'); [Microsoft.VisualBasic.Interaction]::AppActivate('%TITLE%');"
endlocal
goto :eof